package com.qf.joint.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.joint.pojo.Jobhunter;
import com.qf.joint.pojo.Share;
import com.qf.joint.service.JobhunterService;
import com.qf.joint.utils.Info;
import com.qf.joint.utils.SYS;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/jobhunter")
@CrossOrigin
public class JobhunterController extends HttpServlet {
    @Autowired
    private JobhunterService jobhunterService;

    //前台注册
    @RequestMapping(value = "/regist/{phone}/{pass}", method = RequestMethod.GET)
    @ResponseBody
    public Info<Jobhunter> regist(@PathVariable("phone") String phone, @PathVariable("pass") String pass) {
        Info<Jobhunter> info = new Info<>();
        List<Jobhunter> list = jobhunterService.selectByCon(phone, pass);
        if (null != list && list.size() > 0) {
            info.setCode(-2);
            info.setMsg("您已经注册过了！");
            return info;
        }
        Jobhunter jobhunter = new Jobhunter();
        jobhunter.setPhone(phone);
        jobhunter.setPass(pass);
        int res = jobhunterService.insertOne(jobhunter);
        if (res == 1) {
            info.setCode(200);
            info.setMsg("注册成功");
        } else {
            info.setCode(-1);
            info.setMsg("注册失败");
        }
        return info;
    }

    //前台登录
        @RequestMapping(value = "/login/{phone}/{pass}", method = RequestMethod.GET)
    @ResponseBody
    public Info<Jobhunter> login(@PathVariable("phone") String phone, @PathVariable("pass") String pass, HttpSession session) {
        List<Jobhunter> list = jobhunterService.selectByCon(phone, pass);
        Info<Jobhunter> info = new Info<>();
        if (null != list && list.size() == 1) {
            // 将登录用户信息存入session中
            session.setAttribute(SYS.SYS_USER, list.get(0));
            info.setCode(200);
            info.setMsg("登录成功");
        } else {
            System.out.println("if (null==user) {");
            // 把错误信息返回登录页面
            info.setCode(-1);
            info.setMsg("信息输入有误，请重新输入！");
        }
        return info;
    }

    //前台注销
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    @ResponseBody
    public Info<Jobhunter> logout(HttpSession session) {
        session.invalidate();
        Info<Jobhunter> info = new Info<>();
        info.setCode(200);
        info.setMsg("注销成功");
        return info;
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ResponseBody
    public Info<Jobhunter> testSession(HttpSession session) {
        Object obj = session.getAttribute(SYS.SYS_USER);
        if (null != obj && obj instanceof Jobhunter) {
            Jobhunter jobhunter = (Jobhunter) obj;
            System.out.println("phone=" + jobhunter.getPhone() + "；pass=" + jobhunter.getPass());
        } else {
            System.out.println("没有查找到");
        }
        return null;
    }

    /*根据手机号查询 id,姓名、头像、   结果为1条*/
    @RequestMapping(value = "/findid", method = RequestMethod.GET)
    @ResponseBody
    public List<Jobhunter> selectByPhone(HttpSession httpSession) {
        List<Jobhunter> list = new ArrayList<>();
        //String phone="15809290001";
        Object obj = httpSession.getAttribute(SYS.SYS_USER);
        if (null != obj && obj instanceof Jobhunter) {
            Jobhunter jobhunter = (Jobhunter) obj;
            String phone = jobhunter.getPhone();
            Jobhunter job = jobhunterService.selectByPhone(phone);
            System.out.println(job);
            list.add(job);
            System.out.println(list);
        }
        return list;
    }

    /*点击姓名：显示简历：查询/姓名  /优势  */
    @RequestMapping(value = "/findresume")
    @ResponseBody
    public List<Jobhunter> selectByName(HttpSession httpSession) {
        List<Jobhunter> list = new ArrayList<>();
        //int id = 1;
        Jobhunter jobhunter= (Jobhunter) httpSession.getAttribute(SYS.SYS_USER);
        int id=jobhunter.getJid();
        Jobhunter job = jobhunterService.selectByName(id);
        list.add(job);
        /*Info<Jobhunter> info=new Info<>();
        info.setCode(0);
        info.setData(list);*/
        return list;
    }

    /*点击工作经历，进行查询*/
    @RequestMapping(value = "/findjob")
    @ResponseBody
    public List<Jobhunter> selectjob(HttpSession httpSession) {
        List<Jobhunter> list = new ArrayList<>();
       // int id = 1;
       Jobhunter jobhunter= (Jobhunter) httpSession.getAttribute(SYS.SYS_USER);
        int id=jobhunter.getJid();
        Jobhunter job = jobhunterService.selectjob(id);
        list.add(job);
        /*Info<Jobhunter> info=new Info<>();
        info.setCode(0);
        info.setData(list);*/
        return list;
    }

    /*点击工作经历，进行新增*/
    @RequestMapping(value = "/add")
    @ResponseBody
    /*company,trade,tradetype,department,content,performancejob*/
    public Info add(Jobhunter jobhunter) {

        int res = jobhunterService.add(jobhunter);
        Info info = new Info<>();
        if (res == 1) {
            info.setCode(0);
        } else {
            info.setCode(1);
        }
        return info;
    }

    /*点击工作经历，进行修改*/
    @RequestMapping(value = "update")
    @ResponseBody
    public Info update(Jobhunter jobhunter) {
        int res = jobhunterService.update(jobhunter);
        Info info = new Info<>();
        if (res == 1) {
            info.setCode(0);
        } else {
            info.setCode(1);
        }
        return info;
    }

    /*点击设置：点击账号绑定，查询手机号 jobhunter/findphone  */
    @RequestMapping(value = "/findphone")
    @ResponseBody
    public List<Jobhunter> selectPhone(HttpSession httpSession) {
        List<Jobhunter> list = new ArrayList<>();
        //int id = 1;
       Jobhunter jobhunter= (Jobhunter) httpSession.getAttribute(SYS.SYS_USER);
        int id=jobhunter.getJid();
        System.out.println(id);
        Jobhunter job = jobhunterService.selectPhone(id);
        list.add(job);
        /*Info<Jobhunter> info=new Info<>();
        info.setData(list);*/
        return list;

    }

    /*修改手机号jobhunter/changephone*/
    @RequestMapping(value = "/changephone/{phone}")
    @ResponseBody
    public Info<Jobhunter> updatePhone(@PathVariable("phone") String phone, HttpSession httpSession) {
        Jobhunter jobhunter = new Jobhunter();
        jobhunter.setPhone(phone);
        //int id = 1;
       Jobhunter jobhunters= (Jobhunter) httpSession.getAttribute(SYS.SYS_USER);
        int id=jobhunters.getJid();
        jobhunter.setJid(id);
        jobhunter.setPhone(phone);
        int res = jobhunterService.updatePhone(jobhunter);
        Info<Jobhunter> info = new Info<>();
        if (res == 1) {
            info.setCode(0);

        } else {
            info.setCode(1);
        }
        return info;
    }
    /*后台分页查询*/
    @RequestMapping(value = "/all")
    @ResponseBody
    public Info<Jobhunter> selectAll(@RequestParam(defaultValue = "5") Integer page,
                                     @RequestParam(defaultValue = "5") Integer limit) {
        //int count = jobhunterService.selectAll().size();
        PageHelper.startPage(page,limit);
        List<Jobhunter> jobhunters = jobhunterService.selectAll();
        PageInfo<Jobhunter> pageInfo =new PageInfo<>(jobhunters);
        Info<Jobhunter> info = new Info<>();
        if(jobhunters!=null&jobhunters.size()>0){
        info.setCode(0);
        info.setCount((int) pageInfo.getTotal());
        info.setData(pageInfo.getList());
        info.setMsg("success");
        return info;
        }
        info.setCode(1);
        info.setMsg("err");
        return info;
    }

    /*后台删除*/
    @RequestMapping(value="/delete",method = RequestMethod.GET)
    public String delete(@RequestParam(name = "idArry", required = true) String idArry){
        String[] idlist = idArry.split(",");
        List<String> list = new ArrayList<>();
        for (int i = 0; i < idlist.length; i++) {
            list.add(idlist[i]);
        }
        int res = jobhunterService.deleteOne(list);
        System.out.println("delete ine");
        return "redirect:/admin/admin";
    }

    /*后台新增*/

    @RequestMapping(value = "/insertOne", method = RequestMethod.POST)
    public String insertOne(Jobhunter jobhunter) {
        int insertOne = jobhunterService.insertOne(jobhunter);
        System.out.println(insertOne);
        return "redirect:/admin/admin";
    }

    /*后台修改*/
    @RequestMapping(value = "/updateOne", method = RequestMethod.POST)
    @ResponseBody
    public Info<Jobhunter> updateOne(Jobhunter jobhunter) {
        int update = jobhunterService.updateOne(jobhunter);
        Info<Jobhunter> info = new Info<Jobhunter>();
        info.setCode(update);
        /*if (update == 1) {
            return "修改成功";
        }
        return "修改失败";*/
        return info;

    }
}
